At this stage of the course, you've explored how cursors
can be used to point to the location of the data you
require in a MySQL database. However, it's also important to understand how Python makes
use of the cursor class. The cursor class converts MySQL records to more
Python friendly code. With Python, you can
also change or alter the behavior of your cursors
using cursor subclasses. In this video, you'll explore the cursor class and subclasses and to develop an understanding
of how they work. Little Lemon need to
find out how much one of their guests
spent on their meal. They can query this data
from their MySQL database, in a more efficient
manner by using cursor subclasses to create
their Python strings. Let's take a few moments
to find out more, starting with building
an understanding of what database engineers mean by
the term cursor classes. Cursor classes are a
method of translating communications
between Python and a back-end MySQL database. Python sends SQL statements to a MySQL database in the
form of string objects. Cursor classes take these Python string
objects and parse them into MySQL friendly commands and datatypes that can be
understood by the database. Python then uses the cursor
class when retrieving these results to parse them
into Python friendly code. The cursor class contains
several subclasses which can be used to parse string objects in different ways depending
on your needs. At this stage of the course, you may have seen some
examples of subclasses in action in the form of
attributes and methods. For example, the column
names cursor attribute returns the column name of a result set from
a SQL statement. Rowcount returns an integer that represents the number of
rows affected by a select, insert or update statement or there's the execute method. The execute method is the
most common cursor function. It binds the parameters of
a Python string argument to a MySQL query
statement so that it can be executed on
a MySQL database. Cursor subclasses
inherit the properties of the parent cursor class. In turn, the subclasses vary the parent class to improve
the efficiency of the code. Let's explore some examples
of cursor subclasses. One example is the
cursor row subclass. This subclass
returns the results of your variable without pre-processing them to more Python friendly
interpretations. It uses less processing power, leaving you free to create custom conversions
of the results. However, the
disadvantage is that it requires more coding to
process the targeted variable. Little Lemon can use
the cursor row subclass to create their own custom
datatype conversion. This could save time if the initial conversion type
is not the one required. Another example of a subclass is the MySQL cursor
dictionary class. This returns each
row as a dictionary, which helps with
accessing variables. You can access variables by
using direct variable names. Little Lemon can make
use of this subclass to return a result set in
the form of a dictionary. This lets them use the
actual column names of the database columns instead of working through a list
of unnamed tuples. Finally, there's the
buffered cursor class, which takes a subset of data and stores it in a
buffered memory. The advantage to this
subclass is that your code doesn't need to repeatedly request each row
from the server. The disadvantage is that the data needs to be
stored on local memory. You can only use this subclass
to return small datasets. Little Lemon can use a
buffered cursor class to retrieve data. This lets them make
interleaving SQL requests. Interleaving of SQL requests
is when you take part of a SQL query result and use it to make a subsequent
request from a database. Let's take an example
where Little Lemon need to find out how much a
guest spent on a meal. They can interleave a SQL
request to carry out this task. For their first query, they create a MySQL query as a Python string that retrieves
the guest's booking ID. Once they have the result
of the first query, they then create a second
or subsequent query that uses part of
the first result, which is the booking ID, to find the cost of the meal. In other words, Little Lemon can use part of
their first query within their second
query to make a subsequent request
from the database. A database can return multiple results from
the first query. If you use the
first result within your subsequent query before all other results are returned, then MySQL encounters an error called unread result found. It's best practice to finish your loop and let all results print from the first query before you make any
subsequent queries. However, you can
avoid this if you first buffer the results
using a buffered cursor. The buffered cursor
returns all rows, while a standard cursor
requires you to send an individual query
to each effected row. Now that you're familiar with the different cursor subclasses, let's look at the syntax
for instantiating them. The syntax is very similar
across all subclasses. You just pass the
keyword argument to the cursor that alters its
behavior in a particular way. To create a standard cursor, you create the
cursor as an object. To instantiate an instance
of a cursor subclass, you add the subclass as a keyword argument that alters the behavior
of the cursor. For example, you can pass buffered as the keyword to
create a buffered cursor, or pass row as the keyword
to create a row cursor, or dictionary to use
a dictionary cursor. Little Lemon can use a
cursor subclass to request all data from the orders
table in the MySQL database. They can create two
cursor instances, one buffered and the other
a standard implementation. They pass their SQL
select statement as an argument to both cursors. Once executed, the cursors return all items from
the orders table. You should now
understand the concept of cursor subclasses
and how they can be used to alter or change the behavior of a
cursor. Great work.